import  mongoDB from '../mongodb';
import $ from '../utils';
import Mailer from '../nodeMail';


module.exports.GetMainDaily = async function (req,res){
    let queryData = {"deleteStatus":0};
    let rows = req.body.rows;
    let skip = req.body.skip;
    let result = await mongoDB.mongoFunc.findDataPage(queryData,'f_message_daily',rows,skip);

    let buffer = [];
    for( let i = 0, len = result.length; i < len; i++) {
        //console.log(result[i].daily_id)
        let comment = await  mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id,"deleteStatus":0},'f_message_comment');
        //console.log('2',comment)
        let admire = await  mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id},'f_message_admire');
        //console.log('3',admire)
        let fileResult = await mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id},'f_message_file');
        //console.log('4',fileResult)
        let fileList = [];
        if(fileResult.length>0){
            let fileObject = {"imgUrl":fileResult[0].imgUrl,"width":fileResult[0].imgWidth,"height":fileResult[0].imgHeight};
            //console.log(fileObject,fileResult)
            fileList.push(fileObject);
        }


        let object = {'username':result[i].username,'createTime':result[i].serverTime,'user_id':result[i].user_id,'title':result[i].title,
                      'content':result[i].content,'daily_id':result[i].daily_id,'commentSize':comment.length,'admireSize':admire.length,'fileList':fileList}



        buffer.push(object)
    }

    res.send(JSON.stringify({"status":'1',"msg":'获取日志成功',"dataset":buffer}))

    //console.log(buffer)

}

module.exports.CreateDaily = async function (req,res){
    let md5String = req.body.username+$.base.currentTime()+req.body.content;
    let dailyID =  await $.baseFunc.md5(md5String);

    let insertData = {"title":req.body.title,"username":req.body.username,"content":req.body.content,'createTime':req.body.createTime,
                      'serverTime':$.base.currentTime(),'deleteStatus':0,'daily_id':dailyID,'user_id':req.body.userId};

    if(req.body.fileList.length>0){
        let fileData = {"daily_id":dailyID,"imgWidth":req.body.fileList[0].width,"imgHeight":req.body.fileList[0].height,"imgUrl":req.body.fileList[0].imgUrl}
        //console.log('req.body',req.body)
        let result1 = await mongoDB.mongoFunc.insertData(insertData,'f_message_daily');
        let result2 = await mongoDB.mongoFunc.insertData(fileData,'f_message_file');
        if(result1 && result2){
            Mailer.mailer.sendMail(insertData);
            res.send(JSON.stringify({"status":'1',"msg":'日志创建成功',"dataset":[]}));
        }else{
            res.send(JSON.stringify({"status":'0',"msg":'日志创建失败',"dataset":[]}));
        }
    }else{
        let result1 = await mongoDB.mongoFunc.insertData(insertData,'f_message_daily');
        if(result1){
            Mailer.mailer.sendMail(insertData);
            res.send(JSON.stringify({"status":'1',"msg":'日志创建成功',"dataset":[]}));
        }else{
            res.send(JSON.stringify({"status":'0',"msg":'日志创建失败',"dataset":[]}));
        }
    }





}

module.exports.DeleteDaily = async function (req,res){
    let queryData = {"daily_id":req.body.dailyId};
    let updateData = {"deleteStatus":1};

    console.log(queryData)

    let result = await mongoDB.mongoFunc.updateData(queryData,updateData,'f_message_daily');

    if(result){
        res.send(JSON.stringify({"status":'1',"msg":'删除成功',"dataset":[]}))
    }else{
        res.send(JSON.stringify({"status":'0',"msg":'删除失败',"dataset":[]}))
    }
}


module.exports.GetDailyDetail = async function (req,res){
    let queryData = {"daily_id":req.body.dailyId};
    let result = await mongoDB.mongoFunc.findData(queryData,'f_message_daily');

    let buffer = [];
    for( let i = 0, len = result.length; i < len; i++) {
        //console.log(result[i].daily_id)
        let commentList = [];
        let fileList = [];
        let admireList = [];
        let comment = await  mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id,"deleteStatus":0},'f_message_comment');
        let admire = await mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id},'f_message_admire');
        let fileResult = await mongoDB.mongoFunc.findData({"daily_id":result[i].daily_id},'f_message_file');
        //console.log('4',fileResult)

        if(fileResult.length>0){
            let fileObject = {"imgUrl":fileResult[0].imgUrl,"width":fileResult[0].imgWidth,"height":fileResult[0].imgHeight};
            //console.log(fileObject,fileResult)
            fileList.push(fileObject);
        }
        for( let j = 0, len = comment.length; j < len; j++) {
            let userData = await mongoDB.mongoFunc.findData({"username":comment[j].username},'f_message_user');
            let CommentObject = {'username':comment[j].username,'content':comment[j].content,'createTime':comment[j].serverTime,
                                 'comment_id':comment[j].comment_id,'toUsername':comment[j].to_username,'toUserId':comment[j].to_userId,
                                 'user_image':userData[0].user_image_url};
            commentList.push(CommentObject)
        }
        for(let k = 0,len = admire.length;k < len; k++){
            let AdmireObject = {'username':admire[k].username,'user_id':admire[k].user_id,'createTime':admire[k].serverTime};
            admireList.push(AdmireObject)
        }

        let object = {'username':result[i].username,'createTime':result[i].serverTime,'user_id':result[i].user_id,
                      'content':result[i].content,'daily_id':result[i].daily_id,'admireList':admireList,'commentList':commentList,
                      'title':result[i].title,'fileList':fileList}
        buffer.push(object)
    }

    res.send(JSON.stringify({"status":'1',"msg":'获取日志成功',"dataset":buffer}))

}


